#!/bin/sh
final="$@"

EXT=so
SIGNAL=SIGINT
if [ $(uname) = "Darwin" ]; then
    EXT=dylib
    SIGNAL=INT
fi

# add ros path plugin if it does not already exist in the passed in arguments
if [ `expr "$final" : ".*libgazebo_ros_paths_plugin\.$EXT.*"` -eq 0 ]
then
    final="$final -s `catkin_find --first-only libgazebo_ros_paths_plugin.$EXT`"
fi

# add ros api plugin if it does not already exist in the passed in arguments
if [ `expr "$final" : ".*libgazebo_ros_api_plugin\.$EXT.*"` -eq 0 ]
then
    final="$final -s `catkin_find --first-only libgazebo_ros_api_plugin.$EXT`"
fi

client_final="-g `catkin_find --first-only libgazebo_ros_paths_plugin.$EXT`"

setup_path=$(pkg-config --variable=prefix gazebo)/share/gazebo/

# source setup.sh, but keep local modifications to GAZEBO_MASTER_URI
desired_master_uri="$GAZEBO_MASTER_URI"
. $setup_path/setup.sh
if [ "$desired_master_uri" = "" ]; then
	desired_master_uri="$GAZEBO_MASTER_URI"
fi

# Combine the commands
GAZEBO_MASTER_URI="$desired_master_uri" gzserver $final & 
GAZEBO_MASTER_URI="$desired_master_uri" gzclient $client_final

# Kill the server
kill -s $SIGNAL $!
